﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EF;
using BL;
using BL.Entities;
using System.Text;

namespace ApriBox.Mvc.Controllers
{
    public class ShareController : Controller
    {
        //
        // GET: /Share/
        FSEntitiesRepository repository = new FSEntitiesRepository();









        [Authorize]
        public ActionResult Shared()
        {

            if (RouteData.Values["id"] != null & Request.IsAjaxRequest())
            {

                int id = Convert.ToInt32(RouteData.Values["id"]);
                string sharedLinkToBase = DateTime.Now.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalMinutes.ToString() + HttpContext.User.GetHashCode().ToString();
                sharedLinkToBase = sharedLinkToBase.Replace('.', 'z');
              
                if (id>0)
                {
                    if (repository.GetLinkForSharedFile(id) == null)
                    {

                        bool isOkay = repository.CreateFileSharedLink(id, sharedLinkToBase);
                        if (isOkay)
                        {
                            string sharedLink = @"http://localhost:51390/download/file/" + sharedLinkToBase;
                            return Content(sharedLink, "text/plain", Encoding.Default);
                        }
                    }

                    return Content( @"http://localhost:51390/download/file/"+repository.GetLinkForSharedFile(id), "text/plain", Encoding.Default);
                    
                }

                else
                {
                id *= -1;


                if (repository.GetLinkForSharedFolder(id) == null)
                {
                    bool isOkay = repository.CreateFolderSharedLink(id, sharedLinkToBase);
                    if (isOkay)
                    {
                        string sharedLink = @"http://localhost:51390/download/folder/" + sharedLinkToBase;
                        return Content(sharedLink, "text/plain", Encoding.Default);
                    }
                }
                return Content(@"http://localhost:51390/download/folder/" + repository.GetLinkForSharedFolder(id), "text/plain", Encoding.Default);
                }
                
         
            }
            else
                return new EmptyResult();
        }







        [Authorize]
        public ActionResult Unshare()
        {


            if (RouteData.Values["id"] != null & Request.IsAjaxRequest())
            {

                int id = Convert.ToInt32(RouteData.Values["id"]);
               

                if (id > 0)
                {
                    bool isOkay = repository.DeleteFileSharedLink(id);
                    return new EmptyResult();

                }

                else
                {
                    id *= -1;

                    bool isOkay = repository.DeleteFolderSharedLink(id);
                    return new EmptyResult();
                   
                }

            }
            else
                return new EmptyResult();



         
        }
















        #region Методы для шаринга сылки\аншаринга
       




        //Удалить сылку для шарингового фала
        [Authorize]
        public ActionResult SharedLinkOffFile()
        {

            if (RouteData.Values["id"] != null)
            {
                bool isOkay = repository.DeleteFileSharedLink(Convert.ToInt32(RouteData.Values["id"]));

                if (Request.IsAjaxRequest() & isOkay)
                {
                    return new EmptyResult();
                }

                else
                    return new EmptyResult();
            }
            else
                return new EmptyResult();
        }




        //Удалить сылку для шаринговой папки
        [Authorize]
        public ActionResult SharedLinkOffFolder()
        {

            if (RouteData.Values["id"] != null)
            {
                bool isOkay = repository.DeleteFolderSharedLink(Convert.ToInt32(RouteData.Values["id"]));

                if (Request.IsAjaxRequest() & isOkay)
                {
                    return new EmptyResult();
                }

                else
                    return new EmptyResult();
            }
            else
                return new EmptyResult();
        }
        #endregion



    }
}
